/*
 * @Author: Alan (前端组 王勤兰)
 * @Date: 2019-12-04 14:44:38
 * @LastEditors: Alan (前端组 王勤兰)
 * @LastEditTime: 2019-12-05 13:27:33
 * @companyName: tiandy
 * @Description: 数据字典匹配
 */

class DictionatiesMap {
    apiDictionaties() {
        return new Promise((resolve, reject) => {
            window.vue.$apiAxios.postRequestBody('/dictItem/findAllDictItemBatch', {}, res => {
                if (res.data.statusCode === 'API-COMMON-INF-OK') {
                    resolve({
                        data: res.data.content,
                        status: 'success',
                        msg: '请求成功'
                    });
                } else {
                    reject({
                        data: null,
                        status: 'fail',
                        msg: '接口请求失败'
                    });
                }
            });
        });
    }

    async setDictionaties(typeCode, dictCode) {
        try {
            let result = {
                // 设置返参
                data: null,
                status: 'success'
            };

            if (!window.vue.$mainVue.DIRACTIONARIES) {
                // 未获取到数据字典则重新获取
                let res = await this.apiDictionaties();
                window.vue.$mainVue.DIRACTIONARIES = res.data;
            }
            let dictionaties = window.vue.$mainVue.DIRACTIONARIES;

            // 传typeCode则匹配typeCode
            if (typeCode) dictionaties = dictionaties.filter(x => x.typeCode === typeCode);

            // 传dictCode则匹配dictCode
            if (dictCode) dictionaties = dictionaties.filter(x => x.dictCode === dictCode);
            result.data = dictionaties;
            return result;
        } catch (error) {
            return error;
        }
    }
}

export default new DictionatiesMap();
